<?php


namespace app\home\controller;


use app\utils\Response;

class FileController extends ApiBase
{
    public function upload()
    {
        // 获取上传文件表单字段名
        if (!request()->file()) {
            Response::fail('请选择文件');
        }
        $fileKey = array_keys(request()->file());

        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        $maxSize = config('uploadFileMaxSize') * 1024 * 1024;
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => $maxSize, 'ext' => 'jpg,png,gif,jpeg'])->move('uploads');
        if ($info) {
            $path = str_replace('\\', '/', $info->getSaveName());
            Response::success([
                'url' => env('DOMAIN_PATH') . '/uploads/' . $path
            ]);
        } else {
            // 上传失败获取错误信息
            Response::fail($file->getError());
    }
    }
}